import http from '../utils/http'

// 是否启用本地 Mock（可改为读取环境变量）
const USE_MOCK = true

// 简单的网络延迟模拟
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const respond = async (payload, ms = 300) => { await delay(ms); return { data: payload } }

// 本地内存数据
const mockDb = {
  tasks: [
    { id: 1, title: '数组与算法基础', status: 'pending', type: 'coding', difficulty: 'easy', deadline: '2025-09-30', estimatedTime: 30, description: '完成数组反转与去重两道基础题。' },
    { id: 2, title: 'HTTP 协议测验', status: 'in-progress', type: 'quiz', difficulty: 'medium', deadline: '2025-10-05', estimatedTime: 15, description: '选择题与简答题，覆盖状态码、方法与报文结构。' },
    { id: 3, title: 'Todo 应用小项目', status: 'completed', type: 'project', difficulty: 'medium', deadline: '2025-09-10', estimatedTime: 120, description: '使用 Vue 完成一个具备增删改查的 Todo 应用。' }
  ],
  envs: [
    { id: 1, name: '前端教学镜像', status: 'ready', type: 'Docker', url: 'https://registry.example.com/frontend:latest', description: '包含 Node 18 与常用前端构建工具。' },
    { id: 2, name: 'MySQL 数据库', status: 'ready', type: 'Database', url: 'mysql://10.0.0.2:3306', description: '教学用示例数据库。' }
  ],
  students: [
    { id: 1, username: 'stu001', name: '张三', email: 'zhangsan@example.com', className: '计科2021-1班' },
    { id: 2, username: 'stu002', name: '李四', email: 'lisi@example.com', className: '计科2021-2班' },
    { id: 3, username: 'stu003', name: '王五', email: 'wangwu@example.com', className: '软工2021-1班' }
  ]
}

// 任务相关
export const fetchTasks = (params) => {
  if (USE_MOCK) return respond([...mockDb.tasks])
  return http.get('/api/teacher/tasks', params)
}
export const createTask = async (data) => {
  if (USE_MOCK) {
    const id = Date.now()
    mockDb.tasks.unshift({ id, ...data })
    return respond({ id }, 200)
  }
  return http.post('/api/teacher/tasks', data)
}

// 环境资源相关
export const fetchEnvs = (params) => {
  if (USE_MOCK) return respond([...mockDb.envs])
  return http.get('/api/teacher/envs', params)
}
export const createEnv = async (data) => {
  if (USE_MOCK) {
    const id = Date.now()
    mockDb.envs.unshift({ id, ...data })
    return respond({ id }, 200)
  }
  return http.post('/api/teacher/envs', data)
}

// 学生相关
export const fetchStudents = (params) => {
  if (USE_MOCK) return respond([...mockDb.students])
  return http.get('/api/teacher/students', params)
}
export const createStudent = async (data) => {
  if (USE_MOCK) {
    const id = Date.now()
    mockDb.students.unshift({ id, ...data })
    return respond({ id }, 200)
  }
  return http.post('/api/teacher/students', data)
}
export const updateStudent = async (id, data) => {
  if (USE_MOCK) {
    const index = mockDb.students.findIndex((s) => s.id === id)
    if (index !== -1) mockDb.students[index] = { ...mockDb.students[index], ...data }
    return respond({ id }, 200)
  }
  return http.put(`/api/teacher/students/${id}`, data)
}
export const deleteStudent = async (id) => {
  if (USE_MOCK) {
    const index = mockDb.students.findIndex((s) => s.id === id)
    if (index !== -1) mockDb.students.splice(index, 1)
    return respond({ id }, 200)
  }
  return http.delete(`/api/teacher/students/${id}`)
}

export default {
  fetchTasks,
  createTask,
  fetchEnvs,
  createEnv,
  fetchStudents,
  createStudent,
  updateStudent,
  deleteStudent
}

